package com.gxuwz.endrskyweather.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class WeatherDBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "weather_data.db";
    private static final int DB_VERSION = 2;

    // 地区数据表
    public static final String TABLE_AREA = "area_data";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_AREA_CODE = "area_code";
    public static final String COLUMN_AREA_ID = "area_id";
    public static final String COLUMN_AREA_EN = "area_en";
    public static final String COLUMN_AREA_CN = "area_cn";
    public static final String COLUMN_CITY_EN = "city_en";
    public static final String COLUMN_CITY_CN = "city_cn";
    public static final String COLUMN_PROV_EN = "prov_en";
    public static final String COLUMN_PROV_CN = "prov_cn";

    // 收藏表
    public static final String TABLE_FAVORITE = "favorite_data";
    public static final String COLUMN_FAVORITE_ID = "favorite_id";
    public static final String COLUMN_FAVORITE_AREA_CODE = "favorite_area_code";
    public static final String COLUMN_FAVORITE_AREA_NAME = "favorite_area_cn";
    public static final String COLUMN_FAVORITE_DEFAULT_FLAG = "favorite_default_flag";

    public WeatherDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建地区数据表
        String createAreaTable = "CREATE TABLE " + TABLE_AREA + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_AREA_CODE + " TEXT NOT NULL, " +
                COLUMN_AREA_ID + " TEXT NOT NULL, " +
                COLUMN_AREA_EN + " TEXT, " +
                COLUMN_AREA_CN + " TEXT, " +
                COLUMN_CITY_EN + " TEXT, " +
                COLUMN_CITY_CN + " TEXT, " +
                COLUMN_PROV_EN + " TEXT, " +
                COLUMN_PROV_CN + " TEXT)";
        db.execSQL(createAreaTable);
        // 创建收藏表
        String createFavoriteTable = "CREATE TABLE " + TABLE_FAVORITE + " (" +
                COLUMN_FAVORITE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_FAVORITE_AREA_CODE + " TEXT NOT NULL, " +
                COLUMN_FAVORITE_AREA_NAME + " TEXT NOT NULL, " +
                COLUMN_FAVORITE_DEFAULT_FLAG + " INTEGER DEFAULT 0)";
        db.execSQL(createFavoriteTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_AREA);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITE);
        onCreate(db);
    }
}
